package sk.ownage.p1.featurestore;

import java.io.File;

import android.util.Log;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.LatLng;

public class Landmark {
	private final String TAG="Landmark";
	private static final int EMPTY_LANDMARK=-1;
	private int id;
	private String name="",category="",description="";
	private long timestamp=0;
	private String landmarkRootPath="";
	private LandmarkWriter landmarkWriter;
	private LandmarkReader landmarkReader=null;
	private LandmarkGeometry landmarkGeometry=null;
	private LandmarkMapOverlay mapOverlayStore=null;
	
	public Landmark(){
		this(EMPTY_LANDMARK);
	}
	
	public Landmark(int landmarkId){
		id=landmarkId;
	}
	
	public Landmark(int landmarkId, String landmarkRootPath){
		id=landmarkId;
		this.landmarkRootPath=landmarkRootPath;
	}
	
	public LandmarkWriter getLandmarkWriter(){
		if(id!=EMPTY_LANDMARK){
			try {
				landmarkWriter= new LandmarkWriter(id,(!this.landmarkRootPath.equals(""))?this.landmarkRootPath+"/landmarks":"");
			} catch (Exception e) {
				Log.d(TAG,e.toString());
			}
		}else{
			landmarkWriter = new LandmarkWriter((!this.landmarkRootPath.equals(""))?this.landmarkRootPath+"/landmarks":"");
		}
		id=landmarkWriter.getActualLandmarkId();
		return landmarkWriter;
	}
	
	public LandmarkReader getLandmarkReader(){
		if(id!=EMPTY_LANDMARK){
			try {
				landmarkReader=new LandmarkReader(id,(!this.landmarkRootPath.equals(""))?this.landmarkRootPath+"/landmarks":"");
				name=landmarkReader.readName();
				category=landmarkReader.readCategory();
				timestamp=landmarkReader.readTimestamp();
				description=landmarkReader.readDescription();
				landmarkReader=new LandmarkReader(id,(!this.landmarkRootPath.equals(""))?this.landmarkRootPath+"/landmarks":"");
			} catch (Exception e) {
				Log.d(TAG,e.toString());
			}
		}
		return landmarkReader;
	}
	
	public void addPointOfInterestToLandmarkGeometry(LatLng position,long timestamp){
		if(id!=EMPTY_LANDMARK){
			if(landmarkGeometry==null)
				landmarkGeometry=new LandmarkGeometry(id,(!this.landmarkRootPath.equals(""))?this.landmarkRootPath+"/geometry":"");
			landmarkGeometry.addPointOfInterest(position,timestamp);
		}
	}
	

	public void createGeometry(){
		if(landmarkGeometry==null)
			landmarkGeometry=new LandmarkGeometry(id,(!this.landmarkRootPath.equals(""))?this.landmarkRootPath+"/geometry":"");
	}
	
	public boolean hasGeometry(){
		int folderId = id / LandmarkQuadTreeRoot.DIRECTORY_MAX;
		int fileId = id % LandmarkQuadTreeRoot.DIRECTORY_MAX;;
		String folderPath = (((folderId / 100) > 0) ? "" : "0")
				+ (((folderId / 10) > 0) ? "" : "0") + folderId;
		String filePath = (((fileId / 100) > 0) ? "" : "0")
				+ (((fileId / 10) > 0) ? "" : "0") + fileId;
		File geometryFile=new File((!this.landmarkRootPath.equals(""))?this.landmarkRootPath+"/geometry":LandmarkQuadTreeRoot.rootPath + "/" + folderPath + "/" + filePath + "/4/size.bin");
		return geometryFile.exists();
	}

	public void draw(GoogleMap map,BitmapDescriptor icon){
		if(id!=EMPTY_LANDMARK){
			if(mapOverlayStore==null)
				mapOverlayStore=new LandmarkMapOverlay(id,(!this.landmarkRootPath.equals(""))?this.landmarkRootPath+"/geometry":"");
			mapOverlayStore.drawLandmark(map, icon);
		}
	}
	
	public void clearMap(){
		if(id!=EMPTY_LANDMARK){
			if(mapOverlayStore!=null)
				mapOverlayStore.cleanMap();
		}
	}
	
	public String getLandmarkName(){
		if(id!=EMPTY_LANDMARK){
			if(landmarkReader==null)
				landmarkReader=getLandmarkReader();
		}
		return name;
	}
	
	public String getLandmarkCategory(){
		if(id!=EMPTY_LANDMARK){
			if(landmarkReader==null)
				landmarkReader=getLandmarkReader();
		}
		return category;
	}
	
	public long getLandmarkTimestamp(){
		if(id!=EMPTY_LANDMARK){
			if(landmarkReader==null)
				landmarkReader=getLandmarkReader();
		}
		return timestamp;
	}
	
	public String getLandmarkDescription(){
		if(id!=EMPTY_LANDMARK){
			if(landmarkReader==null)
				landmarkReader=getLandmarkReader();
		}
		return description;
	}
	
	public int getID(){
		return id;
	}

}
